home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Tasks / PedTask.cc < prev   
Encoding:
C/C++ Source or Header  |  2000-06-24  |  2.0 KB  |  138 lines

  1. /*    ==========
  2.  *    PedTask.cc
  3.  *    ==========
  4.  */
  5.  
  6. #include <string.h>
  7.  
  8. #include "NGLIterator.hh"
  9.  
  10. #include "PedTask.hh"
  11. #include "PedChore.hh"
  12.  
  13. PedTask::PedTask(PedTask *inParent)
  14. : mParent(inParent), mName(NULL)
  15. {
  16.     if (mParent) {
  17.         mParent->AddSubtask(this);
  18.     }
  19. }
  20.  
  21. PedTask::~PedTask()
  22. {
  23.     if (mParent) {
  24.         mParent->RemoveSubtask(this);
  25.     }
  26.     delete [] mName;
  27. }
  28.  
  29. const char *
  30. PedTask::Name() const
  31. {
  32.     return mName ? mName : "";
  33. }
  34.  
  35. void
  36. PedTask::SetName(const char *inName)
  37. {
  38.     delete [] mName;
  39.     long size = strlen(inName);
  40.     mName = new char [size + 1];
  41.     strcpy(mName, inName);
  42. }
  43.  
  44. void
  45. PedTask::AddSubtask(PedTask *inTask)
  46. {
  47.     if (inTask != NULL) {
  48.         mSubTasks.Append(inTask);
  49.     }
  50. }
  51.  
  52. void
  53. PedTask::RemoveSubtask(PedTask *inTask)
  54. {
  55.     if (inTask != NULL) {
  56.         mSubTasks.Remove(inTask);
  57.     }
  58. }
  59.  
  60. void
  61. PedTask::ScheduleRepeatChore(PedChore *inChore)
  62. {
  63.     if (inChore != NULL) {
  64.         mRepeatQueue.Append(inChore);
  65.     }
  66. }
  67.  
  68. void
  69. PedTask::UnscheduleRepeatChore(PedChore *inChore)
  70. {
  71.     if (inChore != NULL) {
  72.         mRepeatQueue.Remove(inChore);
  73.     }
  74. }
  75.  
  76. void
  77. PedTask::ScheduleIdleChore(PedChore *inChore)
  78. {
  79.     if (inChore != NULL) {
  80.         mIdleQueue.Append(inChore);
  81.     }
  82. }
  83.  
  84. void
  85. PedTask::UnscheduleIdleChore(PedChore *inChore)
  86. {
  87.     if (inChore != NULL) {
  88.         mIdleQueue.Remove(inChore);
  89.     }
  90. }
  91.  
  92. void
  93. PedTask::DoRepeatChores()
  94. {
  95.     RunQueue(mRepeatQueue);
  96.     NGLIterator<PedTask *> iter(mSubTasks);
  97.     PedTask *subtask;
  98.     while (iter.GetNext(subtask)) {
  99.         subtask->DoRepeatChores();
  100.     }
  101. }
  102.  
  103. void
  104. PedTask::DoIdleChores()
  105. {
  106.     RunQueue(mIdleQueue);
  107.     NGLIterator<PedTask *> iter(mSubTasks);
  108.     PedTask *subtask;
  109.     while (iter.GetNext(subtask)) {
  110.         subtask->DoIdleChores();
  111.     }
  112. }
  113.  
  114. void
  115. PedTask::RunQueue(PedQueueChore &inQueue)
  116. {
  117. /*
  118.     // Check for empty queue
  119.     if (inQueue.Count() == 0) return;
  120.     
  121.     // Mark off the existing chores from any new ones
  122.     inQueue.Append(NULL);
  123.     
  124.     PedChore *chore;
  125.     while (chore = inQueue.Behead()) {
  126.         chore->Perform();
  127.     }
  128.     // We hit the NULL entry, which has already been removed.
  129. */
  130.     
  131.     NGLIterator<PedChore *> iter(inQueue);
  132.     PedChore *chore;
  133.     while (iter.GetNext(chore)) {
  134.         chore->Perform();
  135.     }
  136.     
  137. }
  138.